#include "kdcpluginsconfigpage.h"

#include <interfaces/kdcframegeneratorinterface.h>
#include <KPluginInfo>
#include <QVBoxLayout>

KDCPluginsConfigPage::KDCPluginsConfigPage( QWidget *aParent ) : QWidget( aParent )
{
	pSelector = new KPluginSelector( this );

	QVBoxLayout *mainLayout = new QVBoxLayout( this );
	mainLayout->addWidget( pSelector );
}

KDCPluginsConfigPage::~KDCPluginsConfigPage()
{

}

void KDCPluginsConfigPage::setKDCPlugins( const QList<KDCPluginInterface*> &aPlugins )
{
	QList<KPluginInfo> list;
	QList<KDCPluginInterface*>::const_iterator it;
	KDCConfigPageInterface *page;
	for ( it = aPlugins.constBegin(); it != aPlugins.constEnd(); ++it )
	{
		list.append( (*it)->info() );
		page = (*it)->configPage();
		if ( page )
			emit addPluginPage( page, list.last().name(), KIcon( list.last().icon() ) );
	}
	pSelector->addPlugins( list, KPluginSelector::IgnoreConfigFile, "Plugins" ); 
	pSelector->load();
	pSelector->disconnect();
	connect( pSelector, SIGNAL( changed( bool ) ), this, SLOT( slotChanged( bool ) ) );
}

void KDCPluginsConfigPage::slotChanged( bool aState )
{
	pSelector->blockSignals( true );
	pSelector->load();
	pSelector->blockSignals( false );
}

void KDCPluginsConfigPage::addPlugins( const QList<KPluginInfo> &aPlugins )
{
	QList<KPluginInfo>::const_iterator it;
	for ( it = aPlugins.constBegin(); it != aPlugins.constEnd(); ++it )
		pSelector->addPlugins( QList<KPluginInfo>() << *it, KPluginSelector::IgnoreConfigFile, (*it).category() );
	pSelector->load();
	pSelector->disconnect();
	connect( pSelector, SIGNAL( changed( bool ) ), this, SLOT( slotChanged( bool ) ) );
}
